package com.example.todo;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TaskDetailsActivity extends Activity {
	//Open the database
	DBAdapter myDb;
	public final static String EXTRA_TASK_NAME = "com.example.ToDo.MESSAGE";
	public final static String EXTRA_CATEGORY = "com.example.ToDo.CATEGORY";
	public final static String EXTRA_SUBJECT = "com.example.ToDo.SUBJECT";
	public final static String EXTRA_PROJECT = "com.example.ToDo.PROJECT";
	public final static String EXTRA_DATE = "com.example.ToDo.DATE";
	public final static String EXTRA_LOCATION = "com.example.ToDo.LOCATION";
	public final static String EXTRA_DESCRIPTION = "com.example.ToDo.DESCRIPTION";
	public final static String EXTRA_ID = "com.example.ToDo.ID";
	public  static Long TASK_ID;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_task_details);
		setContentView(R.layout.fragment_task_details);
		Intent intent = getIntent();
		
		//Set the text:
		//Task name
		String message = intent.getStringExtra(MainActivity.EXTRA_TASK_NAME);
		TextView editText = (TextView) findViewById(R.id.view_task);
		editText.setText(message);
		//Category
		String strCategory = intent.getStringExtra(MainActivity.EXTRA_CATEGORY);
		TextView editTextCategory = (TextView) findViewById(R.id.view_category);
		editTextCategory.setText(strCategory);
		//Subject
		String strSubject = intent.getStringExtra(MainActivity.EXTRA_SUBJECT);
		TextView editTextSubject = (TextView) findViewById(R.id.view_subject);
		editTextSubject.setText(strSubject);
		//Project
		String strProject = intent.getStringExtra(MainActivity.EXTRA_PROJECT);
		TextView editTextProject  = (TextView) findViewById(R.id.view_project );
		editTextProject.setText(strProject);
		//Due Date
		String strDate = intent.getStringExtra(MainActivity.EXTRA_DATE);
		TextView editTextDate  = (TextView) findViewById(R.id.view_date );
		editTextDate.setText(strDate);
		//Location
		String strLocation = intent.getStringExtra(MainActivity.EXTRA_LOCATION);
		TextView editTextLocation  = (TextView) findViewById(R.id.view_location );
		editTextLocation.setText(strLocation);
		//Description
		String strDescription = intent.getStringExtra(MainActivity.EXTRA_DESCRIPTION);
		TextView editTextDescription  = (TextView) findViewById(R.id.view_description);
		editTextDescription.setText(strDescription);
		//Task ID
		String strID = intent.getStringExtra(MainActivity.EXTRA_ID);	
		TASK_ID = Long.parseLong(strID);
	}

//START NAVIGATION MENU CODE
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.task_details, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_task_details,
					container, false);
			return rootView;
		}
	}
//END NAVIGATION MENU CODE

	private void openDB() {
		myDb = new DBAdapter(this);
		myDb.open();
	}
	private void closeDB() {
		myDb.close();
	}
	public void onClick_Edit_Task(View view){
		openDB();
		Cursor cursor = myDb.getRow(TASK_ID);
		//Get the data in the fields
		final int id = cursor.getInt(DBAdapter.COL_ROWID);
		final String task_id = Integer.toString(id);
		final String  task_name = cursor.getString(DBAdapter.COL_TASK_NAME);
		final String category = cursor.getString(DBAdapter.COL_CATEGORY);
		final String subject = cursor.getString(DBAdapter.COL_SUBJECT);
		final String project = cursor.getString(DBAdapter.COL_PROJECT);
		final String date = cursor.getString(DBAdapter.COL_DATE);
		final String description = cursor.getString(DBAdapter.COL_DESCRIPTION);
		final String location = cursor.getString(DBAdapter.COL_LOCATION);
		//Put the data in the intent
		Intent intent = new Intent(this, EditTaskActivity.class);
		intent.putExtra(EXTRA_ID,  task_id);
    	intent.putExtra(EXTRA_TASK_NAME,  task_name);
    	intent.putExtra(EXTRA_CATEGORY,  category);
    	intent.putExtra(EXTRA_SUBJECT,  subject);
    	intent.putExtra(EXTRA_PROJECT,  project);
    	intent.putExtra(EXTRA_DATE,  date);
    	intent.putExtra(EXTRA_LOCATION,  location);
    	intent.putExtra(EXTRA_DESCRIPTION,  description);
		startActivity(intent);
		closeDB();
	}
	public void onClick_Delete_Task(View view){
		openDB();
		myDb.deleteRow(TASK_ID);
		closeDB();
		Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
		closeDB();
	}
	public void onClick_Cancel(View view){
		Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
	}
}